import { useStore } from "../hooks/useStore";
import io from "socket.io-client";

export const CubesInfo = () => {
  let socket = io("ws://127.0.0.1:3001", { path: "/socket" });

  const [setAllCubes, cubes] = useStore((state) => [
    state.setAllCubes,
    state.cubes,
  ]);

  const sendCubeInfo2Server = (localCubes) => {
    console.log("send2server", localCubes);
    socket.emit("2serverCubes", localCubes);
  };
  const sendCubeInfo2ServerInterval = (localCubes) => {
    console.log("send2server", localCubes);
    socket.emit("interval2serverCubes", localCubes);
  };

  window.onclick = () => {
    console.log("send info ");
    sendCubeInfo2Server(cubes);
  };
  const resetAllCubes = (newCubesData) => {
    console.log("reset all cubes", newCubesData);
    let newCubes = newCubesData.cubes;
    if (cubes.length !== newCubes.length) {
      setAllCubes(newCubes);
    }
  };
  socket.on("resetCubes", resetAllCubes);
  return <></>;
};
